package online.longzipeng.config.knife4j;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.HandlerMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.Arrays;

/**
 * @Author: lzp
 * @description:
 * @Date: 2023/3/26
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {

	/**
	 * 通用接口展示信息
	 *
	 * @return
	 */
	@Bean
	public ApiInfo appInfo() {
		return new ApiInfoBuilder()
				.title("springboot2.x的demo演示接口示例")
				.description("springboot2.x的接口示例")
				.termsOfServiceUrl("https://www.longzipeng.online/")
				.contact("lzp")
				.version("1.0")
				.build();
	}

	@Bean(value = "apiAll")
	public Docket defaultApi2(ApiInfo appInfo) {
		Docket docket = new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(appInfo)
				//分组名称
				.groupName("1所有接口")
				.select()
				//这里指定Controller扫描包路径
				.apis(RequestHandlerSelectors.basePackage("online.longzipeng.controller"))
				.paths(PathSelectors.any())
				.build();
		return docket;
	}

	@Bean(value = "apiUser")
	public Docket apiUser(ApiInfo appInfo) {
		Docket docket = new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(appInfo)
				//分组名称
				.groupName("user相关接口")
				.select()
				//这里指定Controller扫描包路径
				.apis(RequestHandlerSelectors.basePackage("online.longzipeng.controller"))
				.paths(PathSelectors.regex("/user.*"))
				.build();
		return docket;
	}

	/**
	 * api版本2.x
	 */
	@Bean(value = "apiVersion_2")
	public Docket version2(ApiInfo appInfo) {
		return commonVersionDocket(ApiVersionConstant.version2, appInfo);
	}

	/**
	 * api版本3.x
	 */
	@Bean(value = "apiVersion_3")
	public Docket version3(ApiInfo appInfo) {
		return commonVersionDocket(ApiVersionConstant.version3, appInfo);
	}


	/**
	 * 通用版本分配Docket
	 *
	 * @param groupName 对应 ApiVersionConstant 接口中的版本
	 */
	public Docket commonVersionDocket(String groupName, ApiInfo apiInfo) {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo)
				.groupName(groupName)
				.select()
				.apis(input -> {
					HandlerMethod handlerMethod = input.getHandlerMethod();
					// 先判断类上是否包含传入的group
					ApiVersion apiVersionC = handlerMethod.getBeanType().getAnnotation(ApiVersion.class);
					if (apiVersionC != null && (Arrays.asList(apiVersionC.group()).contains(ApiVersionConstant.All)
							|| Arrays.asList(apiVersionC.group()).contains(groupName))) {
						return true;
					}
					// 判断方法中是否包含传入的group
					ApiVersion apiVersionM = handlerMethod.getMethodAnnotation(ApiVersion.class);
					if (apiVersionM != null && (Arrays.asList(apiVersionM.group()).contains(ApiVersionConstant.All)
							|| Arrays.asList(apiVersionM.group()).contains(groupName))) {
						return true;
					}
					return false;
				})
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.paths(PathSelectors.any())
				.build();
	}

}